package pers.vic.widget.file.controller;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import pers.vic.boot.base.model.BaseResponse;
import pers.vic.widget.file.model.FileModel;

/**
 * @description:
 * @author: Vic.xu
 * @date: 2019年12月28日 下午11:37:12
 */
@Controller
@RequestMapping("/mineFile")
public class MineFileController {

    @Value("${upload.path}")
    private String filPath;

    @GetMapping(value = {"", "/", "/home"})
    public String home(Model model) {
        List<FileModel> list = new ArrayList<FileModel>();
        new File(filPath).listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                String fileName = pathname.getName();
                long size = pathname.length();
                list.add(new FileModel(fileName, size));
                return false;
            }
        });
        model.addAttribute("list", list);
        return "mine-files/index";
    }

    /**
     * 文件列表
     */
    @GetMapping("/list")
    @ResponseBody
    public BaseResponse<?> list() {
        List<FileModel> list = new ArrayList<FileModel>();
        new File(filPath).listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                String fileName = pathname.getName();
                long size = pathname.length();
                list.add(new FileModel(fileName, size));
                return false;
            }
        });
        return BaseResponse.success(list);
    }

    /**
     * 删除文件
     */
    @GetMapping("/delete")
    public BaseResponse<?> delete(String fileName) {
        FileUtils.deleteQuietly(new File(filPath, fileName));
        return BaseResponse.success();
    }

    /**
     * 上传
     */
    @PostMapping("upload")
    @ResponseBody
    public BaseResponse<?> upload(@RequestParam("file") MultipartFile file) {
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filPath, file.getOriginalFilename()));
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error(e.getMessage());
        }
        return BaseResponse.success();
    }

    @RequestMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        File file = new File(filPath, fileName);
        if (!file.exists()) {
            throw new IOException("不存在的路径");
        }
        // response.setContentType("application/force-download");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment; filename=\"" + file.getName() + "\"");
        FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
    }

}
